#!/usr/bin/env sh
# DOCS: https://doc.rust-lang.org/rustc/platform-support/openharmony.html
set -ex

SCRIPT_PATH=$(cd $(dirname $0);pwd)
OHOS_BUILD_TOP=$(dirname ${SCRIPT_PATH})
PREBUILTS_DOWNLOAD_DIR=${OHOS_BUILD_TOP}/../openharmony_prebuilts/
RUST_BIN_DIR=${OHOS_BUILD_TOP}/prebuilts/rustc/linux-x86_64/current/bin/
LLVM_BIN_DIR=${OHOS_BUILD_TOP}/prebuilts/clang/ohos/linux-x86_64/llvm/bin

rust_version=1.68.2

# 1. 下载并解压 sysroot

download_file() {
    local url=$1 unzip_dir=$2 md5 local_file
    md5=`echo $url | md5sum | awk '{print $1}'`
    local_file=${PREBUILTS_DOWNLOAD_DIR}/${md5}.`basename $url`

    if [ ! -f $local_file ]; then
        wget -O ${local_file} $url
    fi

    if [ ! -d $unzip_dir ]; then
        mkdir -p $unzip_dir
    fi

    check_sha256=`curl -s -k ${url}.sha256 | cut -d ' ' -f1`
    mark_file_path=$unzip_dir/$check_sha256

    if [ ! -f $mark_file_path ]; then
        tar -xf $local_file -C $unzip_dir
	if [ $# -eq 4 ]; then
	  rm -rf $4
	  mv $3 $4
	fi
        echo 0 > $mark_file_path
    fi
}

# 1. 准备交叉编译工具链及环境

download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240525/clang-dev-linux-x86_64.tar.bz2" "prebuilts/clang/ohos/linux-x86_64" "prebuilts/clang/ohos/linux-x86_64/clang-dev" "prebuilts/clang/ohos/linux-x86_64/clang-15.0.4"
download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240525/ohos-sysroot-dev.tar.bz2" "prebuilts/clang/ohos/linux-x86_64"
download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240608/rust/rust-1.68.2-dev-x86_64-unknown-linux-gnu.tar.gz" "prebuilts/rustc"
download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240608/rust/rust-std-1.68.2-dev-aarch64-linux-ohos.tar.gz" "prebuilts/rustc"
download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240608/rust/rust-std-1.68.2-dev-armv7-linux-ohos.tar.gz" "prebuilts/rustc"
download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240608/rust/rust-std-1.68.2-dev-loongarch64-linux-ohos.tar.gz" "prebuilts/rustc"
download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240608/rust/rust-std-1.68.2-dev-loongarch64-unknown-linux-ohos.tar.gz" "prebuilts/rustc"
download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240608/rust/rust-std-1.68.2-dev-x86_64-unknown-linux-ohos.tar.gz" "prebuilts/rustc"
download_file "http://oh.loongarch.zhcn.cc:10000/toolchains/20240608/rust/rust-std-1.68.2-dev-x86_64-unknown-linux-gnu.tar.gz" "prebuilts/rustc"

#mkdir -p $HOME/ohos-sdk/native/sysroot/usr/include/loongarch64-linux-ohos/
#cp -r /tmp/sysroot/loongarch64-linux-ohos/usr/include/{asm,bits} $HOME/ohos-sdk/native/sysroot/usr/include/loongarch64-linux-ohos/
#
#mkdir -p $HOME/ohos-sdk/native/sysroot/usr/lib/loongarch64-linux-ohos/
#cp /tmp/sysroot/loongarch64-linux-ohos/usr/lib/* $HOME/ohos-sdk/native/sysroot/usr/lib/loongarch64-linux-ohos/

ln -sf `realpath prebuilts/clang/ohos/linux-x86_64/clang-15.0.4` prebuilts/clang/ohos/linux-x86_64/llvm

for script in prebuilts/rustc/*/install.sh; do
    $script --prefix='' --destdir=prebuilts/rustc/linux-x86_64/current/
done

# 2. 下载 rust 源码
cd $SCRIPT_PATH

if [ ! -d rustc-${rust_version}-src ];then
  git clone --depth=1 --single-branch --recurse-submodules -b la64/${rust_version}/v2 https://github.com/yetist/rust.git rustc-${rust_version}-src
fi

# patches (crates)
# libc: https://github.com/rust-lang/libc/commit/c90efd90013f97693d589d0fdd9157337e82dafb
# compiler-builtins: https://github.com/rust-lang/compiler-builtins/commit/8dc0e28e03bc408fb97200939e3e68c2e30d0081

# 3. 编译 rust 包
# 设置环境变量
export PATH=$LLVM_BIN_DIR:$RUST_BIN_DIR:$PATH

export CC_aarch64_linux_ohos=aarch64-unknown-linux-ohos-clang
export AR_aarch64_linux_ohos=llvm-ar
export CXX_aarch64_linux_ohos=aarch64-unknown-linux-ohos-clang++

export CC_aarch64_unknown_linux_ohos=aarch64-unknown-linux-ohos-clang
export AR_aarch64_unknown_linux_ohos=llvm-ar
export CXX_aarch64_unknown_linux_ohos=aarch64-unknown-linux-ohos-clang++

export CC_armv7_linux_ohos=armv7-unknown-linux-ohos-clang
export AR_armv7_linux_ohos=llvm-ar
export CXX_armv7_linux_ohos=armv7-unknown-linux-ohos-clang++

export CC_armv7_unknown_linux_ohos=armv7-unknown-linux-ohos-clang
export AR_armv7_unknown_linux_ohos=llvm-ar
export CXX_armv7_unknown_linux_ohos=armv7-unknown-linux-ohos-clang++

export CC_loongarch64_linux_ohos=loongarch64-unknown-linux-ohos-clang
export AR_loongarch64_linux_ohos=llvm-ar
export CXX_loongarch64_linux_ohos=loongarch64-unknown-linux-ohos-clang++

export CC_loongarch64_unknown_linux_ohos=loongarch64-unknown-linux-ohos-clang
export AR_loongarch64_unknown_linux_ohos=llvm-ar
export CXX_loongarch64_unknown_linux_ohos=loongarch64-unknown-linux-ohos-clang++

export CC_x86_64_unknown_linux_ohos=x86_64-unknown-linux-ohos-clang
export AR_x86_64_unknown_linux_ohos=llvm-ar
export CXX_x86_64_unknown_linux_ohos=x86_64-unknown-linux-ohos-clang++

TARGETS=aarch64-linux-ohos
TARGETS=$TARGETS,aarch64-unknown-linux-ohos
TARGETS=$TARGETS,armv7-linux-ohos
TARGETS=$TARGETS,armv7-unknown-linux-ohos
TARGETS=$TARGETS,loongarch64-linux-ohos
TARGETS=$TARGETS,loongarch64-unknown-linux-ohos
TARGETS=$TARGETS,x86_64-unknown-linux-ohos
TARGETS=$TARGETS,x86_64-unknown-linux-gnu

cd rustc-${rust_version}-src

./configure --enable-local-rust --enable-profiler --disable-docs --set llvm.download-ci-llvm=false --set build.extended=true --set rust.rpath=true --set rust.lld=true --set rust.use-lld=true
python3 ./x.py dist
python3 ./x.py dist --host='' --target $TARGETS

# 4. 测试 rustc 命令
#cat > /tmp/helloworld.rs <<EOF
#fn main() {
#    println!("Hello World!");
#}
#EOF
#./prebuilts/rustc/linux-x86_64/current/bin/rustc --target loongarch64-linux-ohos \
#  -C linker=prebuilts/clang/ohos/linux-x86_64/llvm/bin/loongarch64-unknown-linux-ohos-clang \
#  /tmp/helloworld.rs
#./prebuilts/rustc/linux-x86_64/current/bin/rustc --target loongarch64-unknown-linux-ohos \
#  -C linker=prebuilts/clang/ohos/linux-x86_64/llvm/bin/loongarch64-unknown-linux-ohos-clang \
#  /tmp/helloworld.rs
